package genetic.q1.functions.sumlist.statements;

import genetic.q1.WithallPhenotype;
import genetic.q1.functions.Program;
import genetic.q1.functions.ProgramObject;
import genetic.q1.functions.Statement;
import genetic.q1.functions.Variable;

public class For extends Statement {

	private boolean started;
	
	private For(ProgramObject[] evals) {
		super("For", evals);
		started = false;
	}

	public For() {
		super("For", null);
	}

	@Override
	public Statement CreateInstance(ProgramObject[] evals) {
		return new For(evals);
	}

	@Override
	public ProgramObject choose(int j, int i, WithallPhenotype withallPhenotype) {
		if (j==0) {
			return withallPhenotype.getFromReadVars(i);
		} else if (j==1) {
			return withallPhenotype.getFromLsizeVars(i);
		}
		return null;
	}

	@Override
	public int execute(Program program) {
		int val1 = ((Variable)evals[0]).getValue();
		int val2 = ((Variable)evals[1]).getValue();
		if (started){
			val1++;
			((Variable)evals[0]).setValue(val1);
		} else {
			((Variable)evals[0]).setValue(0);
			started = true;
		}
		if (val1 < val2){
			return program.getCounter() + 1;
		} else {
			return getParam();
		}
	}

	@Override
	public String getObjectName() {
		return "for("+evals[0].getObjectName()+","+evals[1].getObjectName()+");";
	}
	
	@Override
	public boolean isBeginStatement() {
		return true;
	}

	@Override
	public int getNumVars() {
		return 2;
	}
	
	@Override
	public String toString() {
		return "for (" + evals[0].toString() + "=0; "+evals[0].toString() +"<" + evals[1].toString() + "; " + evals[0].toString()+"++) {  // " + getParam();
	}

	@Override
	public void reset() {
		started = false;
	}
	
}
